import { quest } from "../../utils/http";

// pages/task/task.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: [
      { id: 0, name: "全部", select: true },
      { id: 1, name: "未完成", select: false },
      { id: 2, name: "已完成", select: false },
    ],
    content: [],
    size: 10,
    index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {},
  async activeTitle(e) {
    let { index } = e.target.dataset;
    let content,
      title = this.data.title;
    title.forEach((e) => {
      if (e.id === index) {
        e.select = true;
      } else {
        e.select = false;
      }
    });
    switch (index) {
      case 0:
        content = await quest({
          url: "my/all",
          method: "POST",
        });
        break;
      case 1:
        content = await quest({
          url: "my/unfinished",
          method: "POST",
        });
        break;
      case 2:
        content = await quest({
          url: "my/finished",
          method: "POST",
        });
        break;
    }
    if (content.code == 200) {
      this.setData({
        content: content.data.items,
        title,
        size: 10,
        index,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // let token = wx.getStorageSync("token");
    // if (token == "") {
    //   this.setData({
    //     content: [],
    //   });
    // }
    let content = await this.getDataItem(this.data.index, this.data.size);
    console.log(content);
    if (content.code == 200) {
      this.setData({
        content: content.data.items,
      });
    }
  },
  getDetail(e) {
    let { status, detail } = e.currentTarget.dataset;
    console.log(JSON.stringify(detail));
    if (status == 1) {
      return;
    }
    wx.navigateTo({
      url: "/pages/detail/detail?page=myOrder&detail=" + JSON.stringify(detail),
    });
  },
  async finishOrder(e) {
    console.log(e);
    let data = await quest({
      url: "express/update",
      method: "post",
      data: {
        id: e.currentTarget.dataset.id,
        status: 1,
      },
    });
    if (data.code == 200) {
      wx.showModal({
        title: "提示",
        content: "收货成功",
        success(res) {
          if (res.confirm) {
            wx.navigateBack;
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });

      // let content = await this.getDataItem(this.data.index, this.data.size);
      // console.log(content);
      // this.setData({
      //   content: content.data.items,
      // });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  modify(e) {
    wx.navigateTo({
      url:
        "/pages/modify/modify?page="
        + e.currentTarget.dataset.item.title
        + "&detail=" +
        JSON.stringify(e.currentTarget.dataset.item),
    });
  },
  async delete(e) {
    let data = await quest({
      url: "express/delete/" + e.currentTarget.dataset.id,
      method: "delete",
    });
    if (data.code == 200) {
      let content = this.getDataItem(this.data.index, this.data.size);
      this.setData({
        content: content.data.items,
      });
    }
  },
  async getDataItem(index, size) {
    let content;
    switch (index) {
      case 0:
        content = await quest({
          url: "my/all?size=" + size,
          method: "POST",
        });
        break;
      case 1:
        content = await quest({
          url: "my/unfinished?size=" + size,
          method: "POST",
        });
        break;
      case 2:
        content = await quest({
          url: "my/finished?size=" + size,
          method: "POST",
        });
        break;
    }
    return content;
    // this.setData({
    //   content: content.data.items,
    // });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    let size = this.data.size + 10;
    let index = this.data.index;
    console.log(size);
    let content = await this.getDataItem(index, size);
    if (content.code == 200) {
      this.setData({
        content: content.data.items,
        size,
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
